package com.current.game.first20240506;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

/**
 * @author baopan
 * @createTime 2024/5/6 21:35
 */
public class FirstMain extends JPanel {
    // 游戏板尺寸、方块大小等
    private static final int BOARD_SIZE = 4;
    private static final int TILE_SIZE = 100;
    private int[][] board = new int[BOARD_SIZE][BOARD_SIZE];

    // 构造函数、初始化等...

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 绘制游戏板
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                int value = board[i][j];
                // 根据value绘制相应的方块
                g.setColor(getColorForValue(value)); // 假设有一个方法根据值返回颜色
                g.fillRect(j * TILE_SIZE, i * TILE_SIZE, TILE_SIZE, TILE_SIZE);
            }
        }
    }

    private Color getColorForValue(int value) {
        value = new Random().nextInt(10);
        System.out.println("value:"+value);
        Color[] colors = new Color[]{Color.BLACK, Color.BLUE, Color.GRAY, Color.GREEN, Color.RED, Color.PINK};
        return colors[value % colors.length];
    }

    // 处理键盘输入的方法...

    // 其他游戏逻辑方法...

    // 主程序入口
    public static void main(String[] args) {
        JFrame frame = new JFrame("2048 Game");
        // 设置窗口大小不可变
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(BOARD_SIZE * TILE_SIZE+17, BOARD_SIZE * TILE_SIZE+40);
        frame.add(new FirstMain()); // 添加游戏面板到JFrame
        frame.setVisible(true);

        JFrame frame2 = new JFrame("2048 Game");
        // 设置窗口大小不可变
        frame2.setResizable(false);
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame2.setSize(BOARD_SIZE * TILE_SIZE+17, BOARD_SIZE * TILE_SIZE+40);
        frame2.add(new FirstMain()); // 添加游戏面板到JFrame
        frame2.setVisible(true);
    }
}
